# Maintainer: Andrea Zagli <andrea.zagli.free@gmail.com>

_realname=libical
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
provides=(${MINGW_PACKAGE_PREFIX}-${_realname}-lib)
conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-glib)
replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-glib)
pkgver=3.0.8
pkgrel=4
arch=('any')
pkgdesc="An Open Source implementation of the iCalendar protocols and protocol data units (mingw-w64)"
license=("LGPL2.1")
url="https://github.com/libical/libical"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-db")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-doxygen")
options=('strip' '!debug' 'staticlibs')
source=(https://github.com/${_realname}/${_realname}/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz
        001-win.patch
        003-pkg-config.patch
        004-fix-quotation.patch)
noextract=(${_realname}-${pkgver}.tar.gz)
sha256sums=('09fecacaf75ba5a242159e3a9758a5446b5ce4d0ab684f98a7040864e1d1286f'
            'cf6d15035ca51aba5d84f2714e5fc8f7ae5b066bbb8e6b4b7d3a4f0af583f864'
            'e2cbf27f0e04aef8113708348d906bfcbaf2f4bf8c5ca6629e8bf26c7b3ae90d'
            '42cc2d40f8759a88c004821d6c0ab01bbc21204eb1e4df037bd7ca9118fb577c')

prepare() {
  tar --exclude=ReadMe.md -zxf ${_realname}-${pkgver}.tar.gz

  cd ${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-win.patch
  patch -p1 -i ${srcdir}/003-pkg-config.patch
  patch -p1 -i ${srcdir}/004-fix-quotation.patch
}

build() {
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G "MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config} \
    -DCMAKE_SKIP_RPATH=ON \
    -DCMAKE_CXX_FLAGS="${CXXFLAGS}" \
    -DCMAKE_C_FLAGS="${CFLAGS}" \
    -DGOBJECT_INTROSPECTION=OFF \
    -DENABLE_GTK_DOC=OFF \
    ../${_realname}-${pkgver}

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
